<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" 
	import="java.util.List,java.util.ArrayList,com.telecom.cms.dao.*,com.telecom.cms.dto.*" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
	<title>JSP for SysTeamForm form</title>
	<LINK href="css/default.css" rel=stylesheet>
	<LINK href="css/button.css" rel=stylesheet>
	<LINK href="css/main.css" type=text/css rel=stylesheet>
<script type="text/javascript">
	function opchange(o){
		var obj=document.all.ObjSelect;
		for(i=0;i<obj.length;i++){
			obj.options[i].selected=true;
		}
		document.idaTemplateForm.method.value=o;
		document.idaTemplateForm.submit();
	}
	
	function selTemplate(tmptid){
		document.location.href = 'idaTemplate.do?method=resultTemplate&templateId=' + tmptid;
	}

	function Atomoption (text,value){
		this.text=text;
		this.value=value;
	}
	
	var atoms=new Array();
	
	function ChangeGroup()
	{ 
		var i,iGroupIndex;
		iGroupIndex=document.all.groups.selectedIndex;
		iAtomCount=0; 
		while (atoms[iGroupIndex][iAtomCount]!=null) 
		 iAtomCount++;
		 //计算选定组下的原子个数 
		document.all.SrcSelect.length=0;//清空选项数 
		var obj=document.all.ObjSelect;
		var objCount=obj.length;
		for(i=0;i<=iAtomCount-1;i++){//改变下拉菜单的内容 
			var flag=0;
			for(j=0;j<=objCount-1;j++){
				var ov = obj.options[j].value.split("#");
				if(ov[0]==atoms[iGroupIndex][i].value){
					flag++;
				}
			}
			if(flag==0)
			document.all.SrcSelect.add(new Option(atoms[iGroupIndex][i].text,atoms[iGroupIndex][i].value));
		}
		document.all.SrcSelect.focus();
	}
	
	function moveUp()  
	{ 
	var theObj=document.all.ObjSelect;
	for(var i=1;i<theObj.length;i++)
	 {
	  if( theObj.options[i].selected && !theObj.options[i-1].selected )
	  {
	   theObj.options[i].swapNode(theObj.options[i-1]);//与上面一个交换
	  }
	 }
	}
	  
	function moveDown()
	{ 
	var theObj=document.all.ObjSelect;
	for(var i=theObj.length-2;i>-1;i--)
	 {
	  if( theObj.options[i].selected && !theObj.options[i+1].selected )
	  {
	   theObj.options[i].swapNode(theObj.options[i+1]);
	  }
	 }
	}
	
	function moveLeftOrRight(fromObj,toObj)
	{
	 var lengthOfToObj=toObj.length;
	 for(var i=fromObj.length-1;i>-1;i--)
	 {
	  if(fromObj.options[i].selected)
	  {
	   toObj.add(new Option(fromObj.options[i].text,fromObj.options[i].value),lengthOfToObj);//new option(text,value,true,true)
	   toObj.options[lengthOfToObj].selected=true;
	   fromObj.options[i].removeNode(true);
	  }
	 }
	}
	
	function submitchange(){
		obj=document.changeOutput.ObjSelect;
		for(i=0;i<obj.length;i++){
			obj.options[i].selected=true;
		}
		document.changeOutput.target="rsFrame";
		document.changeOutput.submit();
		window.close();
	}
	
	function changeOrderRule(sel){
		var i=sel.selectedIndex;
		var value=sel.options[i].value;
		var text=sel.options[i].innerHTML;
		vstate=value.split("#");
		tstate=text.split("[");
		if(vstate[1]==null){
		sel.options[i].value=vstate[0]+"#asc";
		sel.options[i].innerHTML=tstate[0]+"[正序]";
		}
		if(vstate[1]=="asc"){
		sel.options[i].value=vstate[0]+"#desc";
		sel.options[i].innerHTML=tstate[0]+"[逆序]";
		}
		if(vstate[1]=="desc"){
		sel.options[i].value=vstate[0];
		sel.options[i].innerHTML=tstate[0];
		}
	}
	
	function searchField(){
		document.all.SrcSelect.length=0;
		var key=document.getElementById("field").value;
		if(key){
			var count=0;
			for(i=0;i<atoms.length;i++){
				data=atoms[i];
				for(j=0;j<atoms[i].length;j++){
					result=atoms[i][j].text.indexOf(key)
					if(result>-1){
						document.all.SrcSelect.add(new Option(atoms[i][j].text,atoms[i][j].value));
						count++;
					}
				}
			}
		}
	}
</script>
</head>
<body>
<html:form action="/idaTemplate">
<table width="770" cellspacing="0" cellpadding="0" border="0" align="center">
<tbody>
	<tr>
		<td width="3" height="31">
			<img height="31" src="./images/table_01.gif" width="3" />
		</td>
		<td background="./images/table_02.gif" height="31">
			<table cellspacing="0" cellpadding="0" width="146" align="left" border="0">
				<tbody>
					<tr>
						<td width="51" background="./images/title_1_bg.gif">
							<img height="31" src="./images/title_1.gif" width="51" />
						</td>
						<td width="95" background="./images/title_1_bg.gif">
							<span class="color_b_666">结果展现模板</span>
						</td>
					</tr>
				</tbody>
			</table>
		</td>
		<td width="3">
			<img height="31" src="./images/table_03.gif" width="3" />
		</td>
	</tr>
	<tr>
		<td background="images/table_04.gif">
			<img height="5" src="images/table_04.gif" width="3">
		</td>
		<td style="PADDING-TOP: 3px" valign="top">
		<logic:notPresent name="idaTemplate" scope="request">
			<table width='100%' border='0' cellspacing='0' cellpadding='2'>
				<tr>
					<td width="100" align="center">选择模板：</td>
					<td align="left">
						<html:select property="idaTemplate.templateId" onchange="selTemplate(this.value);" value="${curTemplate.templateId}">
							<html:option value="">新建模板</html:option>
							<logic:present name="templateList" scope="request">
								<logic:iterate name="templateList" id="t">
									<logic:equal name="t" property="templateTypeId" value="20">
										<html:option value="${t.templateId}" >
											<bean:write name="t" property="templateName"/>
										</html:option>
									</logic:equal>
								</logic:iterate>
							</logic:present>
						</html:select>
					</td>
				</tr>
				<tr>
					<td width="100" align="center">取数类别：</td>
					<td align="left">
						<html:select property="idaTemplate.anlsTypeId" value="${curTemplate.anlsTypeId}">
							<html:option value="10">用户&nbsp;&nbsp;</html:option>
							<html:option value="20">客户&nbsp;&nbsp;</html:option>
						</html:select>
					</td>
				</tr>
				<tr>
					<td width="100" align="center">模板名称：</td>
					<td align="left">
						<html:text property="idaTemplate.templateName" size="90" value="${curTemplate.templateName}" />
					</td>
				</tr>
				<tr>
					<td width="100" align="center">模板描述：</td>
					<td align="left">
						<html:textarea property="idaTemplate.templateDesc" rows="3" cols="70" value="${curTemplate.templateDesc}" />
					</td>
				</tr>
				<tr>
					<td align="left" colspan="2">
						<table width='100%' border='0' cellspacing='0' cellpadding='2'>
							<tr align="center" height="25">
								<td background="images/atom_02.gif" width="20%">结果字段类型：</td>
								<td background="images/atom_02.gif" width="25%">可选择结果字段：</td>
								<td background="images/atom_02.gif" width="10%">&nbsp;</td>
								<td background="images/atom_02.gif" width="25%">已选择结果字段：</td>
								<td background="images/atom_02.gif" width="20%">&nbsp;</td>
							</tr>
							<tr align="center" valign="top">
								<td>
									<select name="groups" id="groups" onchange="ChangeGroup()">
									<%
										IdaDAO idadao = new IdaDAO();
										List grouplist = idadao.findAllAtomGroup();
										int idx = 0;
										for (int i = 0; i < grouplist.size(); i++) {
											SEG_BIZ_GROUP_DEFDTO groupdto = (SEG_BIZ_GROUP_DEFDTO) grouplist.get(i);
											SEG_BOOKMARK_DEFDTO bookmark = idadao.findBookmark(groupdto.getBOOKMARK_ID());
											if (bookmark.getCLASS_ID() != 1 && bookmark.getCLASS_ID() != 2)
												continue;
									%>
										<option value=<%=groupdto.getATOM_GROUP_ID()%>><%=bookmark.getBOOKMARK_NAME() + "→" + groupdto.getATOM_GROUP_NAME()%></option>
										<script type="text/javascript">atoms[<%=idx%>]=new Array(); </script>
										<%
											List groupAtoms = idadao.findGroupAtoms(groupdto.getATOM_GROUP_ID());
											if (groupAtoms != null && groupAtoms.size() > 0)
												for (int j = 0; j < groupAtoms.size(); j++) {
													SEG_BIZ_ATOM_DEFDTO showdto = (SEG_BIZ_ATOM_DEFDTO)groupAtoms.get(j);
										%>
										<script type="text/javascript">atoms[<%=idx%>][<%=j%>]=new Atomoption('<%=showdto.getBIZ_ATOM_NAME()%>',<%=showdto.getBIZ_ATOM_ID()%>) </script>
									<%
											}
											idx++;
										}
									%>
									</select>&nbsp;
									<br/>
									搜索字段：
									<input type="text" name="field" onpropertychange="searchField()" size="15">
								</td>
								<td>
									<select name="SrcSelect" size=15 style="font-size: 10pt; width: 200px;" multiple>
									</select>
								</td>
								<td align="center" valign="center">
									<a href="javascript:moveLeftOrRight(document.all.SrcSelect,document.all.ObjSelect)"><img src="images/button_add_arrow.gif" border="0"></a>
									<br><br>
									<a href="javascript:moveLeftOrRight(document.all.ObjSelect,document.all.SrcSelect)"><img src="images/button_del_arrow.gif" border="0"></a>
								</td>
								<td>
									<select name="ObjSelect" ondblclick="changeOrderRule(this)" size=15 style="font-size: 10pt; width: 200px;" multiple>
									</select>
								</td>
								<td align="left">
									<a href="javascript:moveUp()"><img src="images/button_up.gif" border="0"></a>
									<br><br>
									<a href="javascript:moveDown()"><img src="images/button_down.gif" border="0"></a>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<div align="center" id="commit">
							<img src='images/button_addnew.gif' width="63" height="21" onclick="opchange('insertTemplate')" />
							&nbsp;&nbsp;
							<img src='images/button_cancel.gif' width="63" height="21" onclick="opchange('resultTemplate')" />
						</div>
					</td>
				</tr>
			</table>
		</logic:notPresent>
		
		<logic:present name="idaTemplate" scope="request">
			<table width='100%' border='0' cellspacing='' cellpadding='2'>
				<tr>
					<td width="100" align="center">选择模板：</td>
					<td align="left">
						<html:select property="idaTemplate.templateId" onchange="selTemplate(this.value);" value="${idaTemplate.templateId}">
							<html:option value="">新建模板</html:option>
							<logic:present name="templateList" scope="request">
								<logic:iterate name="templateList" id="t">
									<logic:equal name="t" property="templateTypeId" value="20">
										<html:option value="${t.templateId}" >
											<bean:write name="t" property="templateName"/>
										</html:option>
									</logic:equal>
								</logic:iterate>
							</logic:present>
						</html:select>
					</td>
				</tr>
				<tr>
					<td width="100" align="center">取数类别：</td>
					<td align="left">
						<html:select property="idaTemplate.anlsTypeId" value="${idaTemplate.anlsTypeId}">
							<html:option value="10">用户&nbsp;&nbsp;</html:option>
							<html:option value="20">客户&nbsp;&nbsp;</html:option>
						</html:select>
					</td>
				</tr>
				<tr>
					<td width="100" align="center">模板名称：</td>
					<td align="left">
						<html:text property="idaTemplate.templateName" value="${idaTemplate.templateName}" size="90" />
					</td>
				</tr>
				<tr>
					<td width="100" align="center">模板描述：</td>
					<td align="left">
						<html:textarea property="idaTemplate.templateDesc" value="${idaTemplate.templateDesc}" rows="3" cols="70" />
					</td>
				</tr>
				<tr>
					<td align="left" colspan="2">
						<table width='100%' border='0' cellspacing='0' cellpadding='2'>
							<tr align="center" height="25">
								<td background="images/atom_02.gif" width="20%">结果字段类型：</td>
								<td background="images/atom_02.gif" width="25%">可选择结果字段：</td>
								<td background="images/atom_02.gif" width="10%">&nbsp;</td>
								<td background="images/atom_02.gif" width="25%">已选择结果字段：</td>
								<td background="images/atom_02.gif" width="20%">&nbsp;</td>
							</tr>
							<tr align="center" valign="top">
								<td>
									<select name="groups" id="groups" onchange="ChangeGroup()">
									<%
										IdaDAO idadao = new IdaDAO();
										List grouplist = idadao.findAllAtomGroup();
										int idx = 0;
										for (int i = 0; i < grouplist.size(); i++) {
											SEG_BIZ_GROUP_DEFDTO groupdto = (SEG_BIZ_GROUP_DEFDTO) grouplist.get(i);
											SEG_BOOKMARK_DEFDTO bookmark = idadao.findBookmark(groupdto.getBOOKMARK_ID());
											if (bookmark.getCLASS_ID() != 1 && bookmark.getCLASS_ID() != 2)
												continue;
									%>
										<option value=<%=groupdto.getATOM_GROUP_ID()%>><%=bookmark.getBOOKMARK_NAME() + "→" + groupdto.getATOM_GROUP_NAME()%></option>
										<script type="text/javascript">atoms[<%=idx%>]=new Array(); </script>
										<%
											List groupAtoms = idadao.findGroupAtoms(groupdto.getATOM_GROUP_ID());
											if (groupAtoms != null && groupAtoms.size() > 0)
												for (int j = 0; j < groupAtoms.size(); j++) {
													SEG_BIZ_ATOM_DEFDTO showdto = (SEG_BIZ_ATOM_DEFDTO)groupAtoms.get(j);
										%>
										<script type="text/javascript">atoms[<%=idx%>][<%=j%>]=new Atomoption('<%=showdto.getBIZ_ATOM_NAME()%>',<%=showdto.getBIZ_ATOM_ID()%>) </script>
									<%
											}
											idx++;
										}
									%>
									</select>&nbsp;
									<br/>
									搜索字段：
									<input type="text" name="field" onpropertychange="searchField()" size="15">
								</td>
								<td>
									<select name="SrcSelect" size=15 style="font-size: 10pt; width: 200px;" multiple>
									</select>
								</td>
								<td align="center" valign="center">
									<a href="javascript:moveLeftOrRight(document.all.SrcSelect,document.all.ObjSelect)"><img src="images/button_add_arrow.gif" border="0"></a>
									<br><br>
									<a href="javascript:moveLeftOrRight(document.all.ObjSelect,document.all.SrcSelect)"><img src="images/button_del_arrow.gif" border="0"></a>
								</td>
								<td>
									<select name="ObjSelect" ondblclick="changeOrderRule(this)" size=15 style="font-size: 10pt; width: 200px;" multiple>
										<%
											IdaTemplate template = (IdaTemplate)request.getAttribute("idaTemplate");
											List atomList = template.getAtomList();
											for (int i = 0; i < atomList.size(); i++){
												IdaTemplateAtomResult atom = (IdaTemplateAtomResult)atomList.get(i);
												String vo = "";
												String to = atom.getResultOrderRule();
												if(to.equals("asc")){
													to="正序";
												}else if(to.equals("desc")){
													to="逆序";
												}
												if (atom.getResultOrderRule() != null && !atom.getResultOrderRule().equals("")){
													vo = "#" + atom.getResultOrderRule();
													to = "[" + to + "]";
												}
										%>
											<option value="<%=atom.getBizAtomId() + vo%>"><%=atom.getBizAtomName() + to%></option>
										<% } %>
									</select>
								</td>
								<td align="left">
									<a href="javascript:moveUp()"><img src="images/button_up.gif" border="0"></a>
									<br><br>
									<a href="javascript:moveDown()"><img src="images/button_down.gif" border="0"></a>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td align="center" colspan="2">
						<div align="center" id="commit">
							<img src='images/button_modify.gif' width="63" height="21" onclick="opchange('updateTemplate')" />
							&nbsp;&nbsp;
							<img src='images/button_delete.gif' width="63" height="21" onclick="opchange('deleteTemplate')" />
							&nbsp;&nbsp;
							<img src='images/button_exit.gif' width="63" height="21" onclick="opchange('resultTemplate')" />
						</div>
					</td>
				</tr>
			</table>
		</logic:present>
		</td>
		<td background="images/table_05.gif">
			<img height="5" src="images/table_05.gif" width="3">
		</td>
	</tr>
	<tr height="3">
		<td height="3">
			<img height="12" src="images/table_06.gif" width="3">
		</td>
		<td height="3">
			<img height="3" src="images/table_07.gif" width="764">
		</td>
		<td height="3">
			<img height="12" src="images/table_08.gif" width="3">
		</td>
	</tr>
</tbody>
</table>
<br />
<html:hidden property="method" value="resultTemplate" />
<html:hidden property="idaTemplate.templateTypeId" value="20" />
<html:hidden property="idaTemplate.latnId" value="${Latn_Id}" />
</html:form>
<script type="text/javascript">
	document.all.groups.selectedIndex=0;
	ChangeGroup();
</script>
</body>
</html>